package org.br.erp.rfq.service.impl;

import org.br.erp.rfq.entity.Scmquto;
import org.br.erp.rfq.entity.Scmqutoitem;
import org.br.erp.rfq.mapper.ScmqutoitemMapper;
import org.br.erp.rfq.mapper.ScmqutoMapper;
import org.br.erp.rfq.service.IScmqutoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 报价单
 * @Author: jeecg-boot
 * @Date:   2023-01-27
 * @Version: V1.0
 */
@Service
public class ScmqutoServiceImpl extends ServiceImpl<ScmqutoMapper, Scmquto> implements IScmqutoService {

	@Autowired
	private ScmqutoMapper scmqutoMapper;
	@Autowired
	private ScmqutoitemMapper scmqutoitemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Scmquto scmquto, List<Scmqutoitem> scmqutoitemList) {
		scmqutoMapper.insert(scmquto);
		if(scmqutoitemList!=null && scmqutoitemList.size()>0) {
			for(Scmqutoitem entity:scmqutoitemList) {
				//外键设置
				entity.setParentid(scmquto.getId());
				scmqutoitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Scmquto scmquto,List<Scmqutoitem> scmqutoitemList) {
		scmqutoMapper.updateById(scmquto);
		
		//1.先删除子表数据
		scmqutoitemMapper.deleteByMainId(scmquto.getId());
		
		//2.子表数据重新插入
		if(scmqutoitemList!=null && scmqutoitemList.size()>0) {
			for(Scmqutoitem entity:scmqutoitemList) {
				//外键设置
				entity.setParentid(scmquto.getId());
				scmqutoitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		scmqutoitemMapper.deleteByMainId(id);
		scmqutoMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			scmqutoitemMapper.deleteByMainId(id.toString());
			scmqutoMapper.deleteById(id);
		}
	}
	
}
